﻿package com.bigstar.lolkor;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ItemList extends ListActivity {
	
	public static int type = 0;
	public static int attr = 0;
	
	String[] items;
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
        items = new String[ItemData.itemOrder[type][attr].length];
        for(int i = 0; i < ItemData.itemOrder[type][attr].length; i++){
            items[i] =  ItemData.items.get(ItemData.itemOrder[type][attr][i]-1).name_en + " " +ItemData.items.get(ItemData.itemOrder[type][attr][i]-1).name_kor+
            "("+ItemData.items.get(ItemData.itemOrder[type][attr][i]-1).price+"Gold)";
        }
		setListAdapter(new ListAdapter(this));
		setContentView(R.layout.item_list); 
    }

	@Override
    public void onListItemClick(ListView parent, View v, int position, long id) {
        if(getIntent().getExtras() != null){
    		if(getIntent().getExtras().getString("builder").equalsIgnoreCase("item_0")){
    			BuilderItem.item_id[0] = ItemData.itemOrder[type][attr][position]-1;
    		}else if(getIntent().getExtras().getString("builder").equalsIgnoreCase("item_1")){
    			BuilderItem.item_id[1] = ItemData.itemOrder[type][attr][position]-1;
    		}else if(getIntent().getExtras().getString("builder").equalsIgnoreCase("item_2")){
    			BuilderItem.item_id[2] = ItemData.itemOrder[type][attr][position]-1;
    		}else if(getIntent().getExtras().getString("builder").equalsIgnoreCase("item_3")){
    			BuilderItem.item_id[3] = ItemData.itemOrder[type][attr][position]-1;
    		}else if(getIntent().getExtras().getString("builder").equalsIgnoreCase("item_4")){
    			BuilderItem.item_id[4] = ItemData.itemOrder[type][attr][position]-1;
    		}else if(getIntent().getExtras().getString("builder").equalsIgnoreCase("item_5")){
    			BuilderItem.item_id[5] = ItemData.itemOrder[type][attr][position]-1;
    		}
    		Builder.item_select = true;
    		finish();
    		return;
        }
		Intent intent = new Intent(ItemList.this, Item.class);
		intent.putExtra("itemid", ""+(ItemData.itemOrder[type][attr][position]-1));
		startActivity(intent);
	}
	
	
	class ListAdapter extends ArrayAdapter{
		Activity context;
		private LayoutInflater mInflater;
		public ListAdapter(Activity context){
			super(context, R.layout.item_info_row, items);
			this.context = context;
			 mInflater = LayoutInflater.from(context);
		}
		     
		public View getView(int position, View convertView, 
				ViewGroup parent){
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_info_row, null); 
				holder = new ViewHolder();
				holder.item_name = (TextView)convertView.findViewById(R.id.item_name); 
				holder.item_text = (TextView)convertView.findViewById(R.id.item_text); 
				holder.item_img = (ImageView)convertView.findViewById(R.id.item_img); 
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
		    
			holder.item_name.setText(items[position]);
		      
			String text = "";
			if(ItemData.items.get(ItemData.itemOrder[type][attr][position]-1).text != null){
				text =ItemData.items.get(ItemData.itemOrder[type][attr][position]-1).text;
				if(ItemData.items.get(ItemData.itemOrder[type][attr][position]-1).unique!=null){
					text = text + "<br>";
				}
			}
			if(ItemData.items.get(ItemData.itemOrder[type][attr][position]-1).unique!=null){
				text = text + ItemData.items.get(ItemData.itemOrder[type][attr][position]-1).unique;
				text = text.replaceAll(":br:", "<br>");
			}
			holder.item_text.setText(Html.fromHtml(text));
			
		       
			holder.item_img.setImageResource(R.drawable.item_000+ItemData.itemOrder[type][attr][position]-1);
			
			return convertView;
		}
	}
	
	 static class ViewHolder {
		 ImageView item_img;
		 TextView item_name;
		 TextView item_text;
			
		 //TextView tvGoodsLocation;
	 }
}
